package com.gitee.magic.jdbc.persistence.support;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.gitee.magic.core.exception.ApplicationException;
import com.gitee.magic.jdbc.persistence.EntityFactory;
import com.gitee.magic.jdbc.persistence.EntityObject;
import com.gitee.magic.jdbc.persistence.annotation.Entity;

/**
 * @author start
 */
public class DefaultEntityFactory implements EntityFactory {

	private Map<String, EntityObject> entitys = new ConcurrentHashMap<String, EntityObject>();

	/**
	 * 获取实体类
	 * @param prototype
	 * @return
	 */
	@Override
	public EntityObject getEntityDefinition(Class<?> prototype) {
		Entity entity = prototype.getAnnotation(Entity.class);
		if(entity==null){
			return null;
		}
		return entitys.get(entity.value());
	}

	/**
	 * 注册实体类
	 * 
	 * @param entity
	 */
	@Override
	public void registerEntityDefinition(EntityObject entity) {
		if (entity != null) {
			String name = entity.getName();
			if (entitys.containsKey(name)) {
				throw new ApplicationException("ORM实体类："+name+"--已经注册，重复注册！");
			} else {
				entitys.put(name, entity);
			}
		}
	}
	
	@Override
	public Boolean isOrmEntity(Class<?> prototype){
		return prototype.isAnnotationPresent(Entity.class);
	}

}
